package com.szyjjk.service;


import com.szyjjk.entity.*;
import com.szyjjk.mapper.RunSunMapper;
import com.szyjjk.mapper.RunTideMapper;
import com.szyjjk.mapper.RunWaterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class RunObserve {

    @Autowired
    private RunTideMapper runTideMapper;

    @Autowired
    private RunSunMapper runSunMapper;

    @Autowired
    private RunWaterMapper runWaterMapper;

    /**
     * @return boolean
     * @Author yangyue
     * @Description //TODO 查询当年有没有数据
     * @Date 10:47 2020/3/31
     * @Param [yeas]
     **/
    public boolean findYearTide(String yeas, String siteID) {
        Calendar calendarO = Calendar.getInstance();
        calendarO.set(Integer.parseInt(yeas), 1, 1, 0, 0, 0);

        Calendar calendarL = Calendar.getInstance();
        calendarL.set(Integer.parseInt(yeas), 12, 31, 23, 59, 59);

        RunTideExample example = new RunTideExample();
        example.createCriteria().andTideDateBetween(calendarO.getTime(), calendarL.getTime()).andSideIdEqualTo(siteID);
        List<RunTide> array = runTideMapper.selectByExample(example);
        if (array.size() == 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @return java.util.List<com.szyjjk.entity.RunTide>
     * @Author yangyue
     * @Description //TODO 根据站点ID 和时间去查询
     * @Date 17:21 2020/4/8
     * @Param [siteId, time]
     **/
    public List<RunTide> findRunTideBySidAndDate(String siteId, Date time) {
        RunTideExample example = new RunTideExample();
        example.createCriteria().andSideIdEqualTo(siteId).andTideDateEqualTo(time);
        return runTideMapper.selectByExample(example);
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunTide>
     * @Author yangyue
     * @Description //TODO 分页查询潮汐数据
     * @Date 15:00 2020/4/14
     * @Param [pageNo, pageSize, siteID, begTime, endTime]
     **/
    public List<RunTide> findPageRunTide(Integer pageNo, Integer pageSize, String siteID, Date begTime, Date endTime) {
        RunTideExample example = new RunTideExample();
        if (begTime == null) {
            example.createCriteria().andSideIdEqualTo(siteID);
        } else {
            example.createCriteria().andSideIdEqualTo(siteID).andTideDateBetween(begTime, endTime);
        }
        example.setOrderByClause(" tide_date desc limit " + pageNo + " , " + pageSize);
        return runTideMapper.selectByExample(example);
    }

    /**
     * @return java.util.List<com.szyjjk.entity.RunTide>
     * @Author yangyue
     * @Description //TODO 返回分页所需的数量
     * @Date 15:01 2020/4/14
     * @Param [siteID, begTime, endTime]
     **/
    public int findPageRunTideCount(String siteID, Date begTime, Date endTime) {
        RunTideExample example = new RunTideExample();
        if (begTime == null) {
            example.createCriteria().andSideIdEqualTo(siteID);
        } else {
            example.createCriteria().andSideIdEqualTo(siteID).andTideDateBetween(begTime, endTime);
        }
        return runTideMapper.selectByExample(example).size();
    }


    /**
     * @return java.lang.Boolean
     * @Author yangyue
     * @Description //TODO 保存 潮汐
     * @Date 11:57 2020/3/31
     * @Param [runTide]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveRunTide(RunTide runTide) {
        try {
            //todo 可填加后续逻辑
            runTideMapper.insertSelective(runTide);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunSun>
     * @Author yangyue
     * @Description //TODO 根据时间和类别。标题查询太阳辐射
     * @Date 11:46 2020/4/2
     * @Param [time, type, title]
     **/
    public int findSunByTimeAndSiteID(Date time, String siteID) {
        RunSunExample example = new RunSunExample();
        example.createCriteria().andSunTimeEqualTo(time).andSiteIdEqualTo(siteID);
        return runSunMapper.countByExample(example);
    }

    /**
     * @Author yangyue
     * @Description //TODO 分页查询太远辐射 
     * @Date 10:23 2020/4/15
     * @Param [pageNo, pageSize, siteID, begTime, endTime]
     * @return java.util.List<com.szyjjk.entity.RunSun>
     **/
    public List<RunSun> findSunPage(Integer pageNo, Integer pageSize, String siteID, Date begTime, Date endTime) {
        RunSunExample example = new RunSunExample();
        if (begTime == null) {
            example.createCriteria().andSiteIdEqualTo(siteID);
        }else{
            example.createCriteria().andSiteIdEqualTo(siteID).andSunTimeBetween(begTime,endTime);
        }
        example.setOrderByClause("sun_time desc limit "+pageNo+" , "+pageSize);
        return runSunMapper.selectByExample(example);
    }
    /**
     * @Author yangyue
     * @Description //TODO 返回太远辐射的数量
     * @Date 10:31 2020/4/15
     * @Param [pageNo, pageSize, siteID, begTime, endTime]
     * @return int
     **/
    public  int findSunPageCount( String siteID, Date begTime, Date endTime) {
        RunSunExample example = new RunSunExample();
        if (begTime == null) {
            example.createCriteria().andSiteIdEqualTo(siteID);
        }else{
            example.createCriteria().andSiteIdEqualTo(siteID).andSunTimeBetween(begTime,endTime);
        }
        return runSunMapper.selectByExample(example).size();
    }






    /**
     * @return java.util.List<com.szyjjk.entity.RunSun>
     * @Author yangyue
     * @Description //TODO
     * @Date 14:57 2020/4/9
     * @Param [siteID]
     **/
    public List<RunSun> findSunBySiteID(String siteID) {
        RunSunExample example = new RunSunExample();
        example.createCriteria().andSiteIdEqualTo(siteID);
        example.setOrderByClause("sun_time  desc");
        return runSunMapper.selectByExample(example);
    }


    /**
     * @return java.lang.Boolean
     * @Author yangyue
     * @Description //TODO 保存太阳辐射数据
     * @Date 11:46 2020/4/2
     * @Param [runSun]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveRunSun(RunSun runSun) {
        try {
            //todo 可填加后续逻辑
            runSunMapper.insertSelective(runSun);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunWater>
     * @Author yangyue
     * @Description //TODO 查询water id 和时间
     * @Date 18:53 2020/4/3
     * @Param [time, id]
     **/
    public int findWaterBySiteIDAndDate(Date time, String id) {
        RunWaterExample example = new RunWaterExample();
        example.createCriteria().andSiteIdEqualTo(id).andWaterTimeEqualTo(time);
        return runWaterMapper.countByExample(example);
    }


    /**
     * @Author yangyue
     * @Description //TODO 分页查询水质数据
     * @Date 15:18 2020/4/14
     * @Param [pageNo, pageSize, siteID, begTime, endTime]
     * @return java.util.List<com.szyjjk.entity.RunWater>
     **/
    public List<RunWater> findPageWater(Integer pageNo, Integer pageSize, String siteID, Date begTime, Date endTime) {
        RunWaterExample example = new RunWaterExample();
        if (begTime == null) {
            example.createCriteria().andSiteIdEqualTo(siteID);
        } else {
            example.createCriteria().andSiteIdEqualTo(siteID).andWaterTimeBetween(begTime,endTime);
        }
        example.setOrderByClause("water_time desc limit "+pageNo+" , "+pageSize);
        return  runWaterMapper.selectByExample(example); 
    }

    /**
     * @Author yangyue
     * @Description //TODO 分页查询返回数据
     * @Date 15:31 2020/4/14
     * @Param [siteID, begTime, endTime]
     * @return int
     **/
     public int findPageWaterPage( String siteID, Date begTime, Date endTime) {
        RunWaterExample example = new RunWaterExample();
        if (begTime == null) {
            example.createCriteria().andSiteIdEqualTo(siteID);
        } else {
            example.createCriteria().andSiteIdEqualTo(siteID).andWaterTimeBetween(begTime,endTime);
        }
        return  runWaterMapper.selectByExample(example).size();
    }


    /**
     * @return java.util.List<com.szyjjk.entity.RunWater>
     * @Author yangyue
     * @Description //TODO 根据站点ID 查询
     * @Date 11:38 2020/4/9
     * @Param [id]
     **/
    public List<RunWater> findWaterBySiteID(String id) {
        RunWaterExample example = new RunWaterExample();
        example.createCriteria().andSiteIdEqualTo(id);
        example.setOrderByClause("water_time  desc");
        return runWaterMapper.selectByExample(example);
    }


    /**
     * @return java.lang.Boolean
     * @Author yangyue
     * @Description //TODO
     * @Date 18:50 2020/4/3
     * @Param [runWater]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveRunWater(RunWater runWater) {
        try {
            //todo 可填加后续逻辑
            runWaterMapper.insertSelective(runWater);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
